<?php

/**
 * Class AccountController
 */
class AccountController extends BaseController
{

	/*
	|--------------------------------------------------------------------------
	| 账户控制器
	|--------------------------------------------------------------------------
	|
	|	Login			登陆界面
	|	GetCaptcha		验证码
	|	DisposeLogin	登陆验证
	|	Logout			注销用户
	|	GetStuffList	员工列表
	|	GetStuffListPart员工列表分页
	|	displayStuffList员工列表模版分发
	|
	*/

    public function Index()
    {
		return Redirect::to('/forest_service');

    }




    /**
     * 商家列表
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowSupplierList()
    {
        $info = AccountService::instance()->ShowSupplierList();
        return $this->view('admin.show_supplier')->with('suppliers',$info);
    }

    /**
 * 商家入驻
 * @author alice
 * @date 2016-1-19
 * @return mixed
 * @throws Exception
 */
//    public function AddSupplier()
//    {
//        if(Request::isMethod('POST'))
//        {
//            #收集信息
//            $aData                       =           new stdClass();
//            $aData->company              =           $this->getParam('company');
//            $aData->supplier_name        =           $this->getParam('supplier_name');
//            $aData->brand_name           =           $this->getParam('brand_name');
//            $aData->place                =           $this->getParam('place');
//            $aData->market               =           $this->getParam('market');
//            $aData->scale                =           $this->getParam('scale');
//            $aData->business_scope       =           $this->getParam('business_scope');
//            $aData->supplier_img1        =           $this->getParam('supplier_img1');
//            $aData->owner                =           $this->getParam('owner');
//            $aData->phone                =           $this->getParam('phone');
//            $aData->operate_license      =           $this->getParam('operate_license');
//            $aData->tex_license          =           $this->getParam('tex_license');
//
//            #逻辑操作
//            AccountService::instance()->AddSupplier($aData);
//            return Redirect::to('/show_supplier');}else{
//            return $this->view('admin.add_supplier')->with('scale',SupplierEnum::$Supplier_scale);
//        }
//    }

    /**
     * 商家信息修改
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function EditSupplier()
    {
        if(Request::isMethod('POST'))
        {
            #收集信息
            $aData                       =           new stdClass();
            $aData->supplier_id          =           $this->getParam('supplier_id');
            $aData->company              =           $this->getParam('company');
            $aData->supplier_name        =           $this->getParam('supplier_name');
            $aData->brand_name           =           $this->getParam('brand_name');
            $aData->place                =           $this->getParam('place');
            $aData->market               =           $this->getParam('market');
            $aData->scale                =           $this->getParam('scale');
            $aData->business_scope       =           $this->getParam('business_scope');
            $aData->supplier_img1        =           $this->getParam('supplier_img1');
            $aData->owner                =           $this->getParam('owner');
            $aData->phone                =           $this->getParam('phone');
            $aData->operate_license      =           $this->getParam('operate_license');
            $aData->tex_license          =           $this->getParam('tex_license');


            #逻辑操作
            AccountService::instance()->EditSupplier($aData);
            return Redirect::to('/show_supplier');}else{
            $id = $this->getParam('supplier_id');
            $info = AccountService::instance()->ShowSupplierInfo($id);
            return $this->view('admin.edit_supplier')->with(['supplier'=>$info, 'scale'=>SupplierEnum::$Supplier_scale]);
        }
    }

    /**
     * 删除商家
     */
    public function DeleteSupplier()
    {
        $supplier_id = $this->getParam('id');
        $result = AccountService::instance()->DeleteSupplierInfo($supplier_id);
        if($result){
            $this->rest->success([],ErrorCodeEnum::STATUS_SUCCESS,'成功啦!');}
        $this->rest->error('删除失败!',ErrorCodeEnum::STATUS_ERROR);
    }







	/**
	 * 登录界面
	 */
	public function Login()
	{
		$user_info = Session::get(UserEnum::USER_INFO_SESSION_KEY);
		if ($user_info) {
			//跳转到默认页
			$page = AccountService::instance()->getDefaultPage($user_info->role_id);
			return Redirect::to($page);
		} else {
			$this->view('account.login');
		}
	}

	/**
	 * 生成验证码
	 * @return array $param
	 */
	public function GetCaptcha()
	{
		return CaptchaService::instance()->create();
	}

	/**
	 * 登陆验证
	 * @return mixed
	 */
	public function DisposeLogin()
	{
		$aData = new stdClass();
		$aData->user_name = $this->getParam('UserName');
		$aData->user_pwd = strtoupper(md5($this->getParam('Password')));
		$aData->captcha = $this->getParam('Captcha');
		try{
			AccountService::instance()->LoginValidator($aData);
		}catch (Exception $e){
			//SeasLog::error('用户登陆失败:message->'.$e->getMessage()."code->".$e->getCode());
			$this->rest->error($e->getMessage(), ErrorCodeEnum::STATUS_ERROR, array());
//			return Redirect::to('/login');
		}
		$user_info = Session::get(UserEnum::USER_INFO_SESSION_KEY);
		$page = AccountService::instance()->getDefaultPage($user_info->role_id);
		return Response::json(array(
			'code' => ErrorCodeEnum::STATUS_SUCCESS,
			'msg'  => '登录成功',
			'data' => array('url' => $page)
		));
//		return Redirect::to($page);
	}

	/**
	 * 注销用户
	 * @return mixed
	 */
	public function Logout()
	{
		if(Session::has(UserEnum::USER_INFO_SESSION_KEY)) {
			Session::flush();
//			Session::forget(UserEnum::USER_INFO_SESSION_KEY);
		}
		return Redirect::to('/login');
	}

	/**
	 * 员工列表
	 */
	public function GetStuffList()
	{
		$stuff_role 	= $this->getParam('stuff_role', UserEnum::USER_ROLE_TEACHER);
		$code_role 		= array_search($stuff_role, UserEnum::$roles);
		$account 		= AccountService::instance()->getAccountByRole($code_role,1);
		$account_number = AccountService::instance()->getStuffNumberByRole($code_role);
		$total_num 		= ceil($account_number/20);
		$i 				= $total_num > 0 ? ToolKit::makePageList(1,$total_num) : '';

		#汇总信息
		$data  = array(
			'account' =>  $account,
			'stuff_number' => $account_number,
			'i'		=> $i
		);

		$this->displayStuffList($code_role, $data, 'list');
	}

	/**
	 * 员工列表分页
	 */
	public function GetStuffListPart()
	{
		$page 			= (int)$this->getParam('page',1);
		$stuff_role 	= $this->getParam('stuff_role', UserEnum::USER_ROLE_TEACHER);
		$code_role 		= array_search($stuff_role, UserEnum::$roles);
		$account 		= AccountService::instance()->getAccountByRole($code_role,$page);
		$account_number = AccountService::instance()->getStuffNumberByRole($code_role);
		$total_num 		= ceil($account_number/20);
		$i 				= $total_num > 0 ? ToolKit::makePageList($page,$total_num) : '';

		#汇总信息
		$data  = array(
			'account' =>  $account,
			'stuff_number' => $account_number,
			'i'		=> $i
		);

		$this->displayStuffList($code_role, $data, 'ajax');
	}

	/**
	 * 员工列表模版分发
	 * @param $code_role
	 * @param $data
	 * @param $type
	 */
	public function displayStuffList($code_role, $data, $type)
	{
		switch($code_role){
			case UserEnum::USER_ROLE_TEACHER:
				($type == 'list') ?
					$this->view('account.teacher_list')->with($data) :
					$this->viewAjax('account.ajaxTemplates.teacher_list_part')->with($data);
				break;
			case UserEnum::USER_ROLE_STUDENT:
				($type == 'list') ?
					$this->view('account.student_list')->with($data):
					$this->viewAjax('account.ajaxTemplates.student_list_part')->with($data);
				break;
			case UserEnum::USER_ROLE_LEADER:
				($type == 'list') ?
					$this->view('account.leader_list')->with($data):
					$this->viewAjax('account.ajaxTemplates.leader_list_part')->with($data);
				break;
			case UserEnum::USER_ROLE_CONTACT:
				($type == 'list') ?
					$this->view('account.contact_list')->with($data):
					$this->viewAjax('account.ajaxTemplates.contact_list_part')->with($data);
				break;
			case UserEnum::USER_ROLE_PROVOST:
				($type == 'list') ?
					$this->view('account.provost_list')->with($data):
					$this->viewAjax('account.ajaxTemplates.provost_list_part')->with($data);
				break;
			case UserEnum::USER_ROLE_ADVANCED_LEADER:
				($type == 'list') ?
					$this->view('account.advanced_leader')->with($data):
					$this->viewAjax('account.ajaxTemplates.advanced_leader_part')->with($data);
				break;
			default:
				break;
		}
	}
}
